package com.jlf.core.engine.domain;

import com.baomidou.mybatisplus.annotation.Version;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jlf.core.annotation.Domain;
import com.jlf.core.domain._SimpleDO;
import com.jlf.core.engine.constant.SchemaConstant;
import com.jlf.tool.toolkit.BitToolkit;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * 领域表定义的模型领域
 *
 * @author wujr
 * 2023/7/5
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/5 1.0 新增]
 */
@Getter
@Setter
@Domain(code = "schemaTable", name = "领域模型定义", table = "e_jlf_schema_domain")
public class SchemaDomainDO extends _SimpleDO<SchemaDomainDO> {
    /**
     * 领域的编码，具有唯一性
     * 用于：注解：{@link Domain#code()}
     */
    private String domainCode;
    /**
     * 领域名称
     * 用于：注解：{@link Domain#name()}
     */
    private String domainName;
    /**
     * 该领域的数据表名称
     * 用于：注解：{@link Domain#table()}
     */
    private String domainTable;
    /**
     * 领域的版本号
     */
    private Integer domainVersion;
    /**
     * 数据库，主键id的类型 {@link SchemaConstant.Jdbc.id}
     */
    private Integer dbIdType;
    /**
     * 领域的包
     */
    private String domainPackage;
    /**
     * 该模型领域的管理接口的路径
     */
    private String restfulPath;
    /**
     * 该领域的记录的乐观锁版本，和domainVersion不是同一个概念
     */
    @Version
    private Integer optimisticVersion;
    /**
     * 是否需要对记录进行乐观锁
     */
    private Boolean optimisticLocker;
    /**
     * 事务标记 {@link SchemaConstant.Jdbc.Transaction}
     */
    private Integer transactionFlag;

    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isAutoIncrement(){
        return SchemaConstant.Jdbc.isAutoIncrement(this.dbIdType);
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public String simpleName(){
        String domainSimpleName = StringUtils.capitalize(this.domainCode);
        if (!domainSimpleName.endsWith("DO")){
            domainSimpleName += "DO";
        }
        return domainSimpleName;
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public String simpleClass(){
        if (this.domainPackage.endsWith(".domain")) {
            return this.domainPackage + "." + simpleName();
        }else{
            return this.domainPackage + ".domain." + simpleName();
        }
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public String daoPackage(){
        if (this.domainPackage.endsWith(".domain")){
            return domainPackage.substring(0, domainPackage.length() - ".domain".length()) + ".dao";
        }else{
            return domainPackage + ".dao";
        }
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public String persistencePackage(){
        if (this.domainPackage.endsWith(".domain")){
            return domainPackage.substring(0, domainPackage.length() - ".domain".length()) + ".persistence";
        }else{
            return domainPackage + ".persistence";
        }
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public String serverPackage(){
        if (this.domainPackage.endsWith(".domain")){
            return domainPackage.substring(0, domainPackage.length() - ".domain".length()) + ".server";
        }else{
            return domainPackage + ".server";
        }
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public String controllerPackage(){
        if (this.domainPackage.endsWith(".domain")){
            return domainPackage.substring(0, domainPackage.length() - ".domain".length()) + ".controller";
        }else{
            return domainPackage + ".controller";
        }
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isLockOptimistic(){
        return Boolean.TRUE.equals(this.optimisticLocker);
    }

    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isAddNeedTransaction(){
        return BitToolkit.isBit(this.transactionFlag, SchemaConstant.Jdbc.Transaction.add - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isEditNeedTransaction(){
        return BitToolkit.isBit(this.transactionFlag, SchemaConstant.Jdbc.Transaction.edit - 1);
    }
    @JsonIgnore
    @JsonIgnoreProperties
    public boolean isDeleteNeedTransaction(){
        return BitToolkit.isBit(this.transactionFlag, SchemaConstant.Jdbc.Transaction.delete - 1);
    }
}
